package com.cn.zaprk;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: com.cn.zaprk
 * @ClassName: AjaxServlet01
 * @Description:
 * @author: RZS
 * @date: 2022/4/29  20:16
 */
@WebServlet("/as01.do")
public class AjaxServlet01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        // 获取请求参数
        String uname = request.getParameter("uname");
        String pwd = request.getParameter("pwd");
        System.out.println(uname + " : " + pwd);

        // 准备响应的数据
        // 定义；list集合
        List<User> list = new ArrayList<>();
        list.add(new User(1, "张三", "123456"));
        list.add(new User(2, "大锤", "123456"));
        list.add(new User(3, "王二狗", "123456"));

        // 将list集合转为json字符串
        String jsonString = JSONObject.toJSONString(list);

        // 响应数据
        response.getWriter().println(jsonString);
    }
}
